﻿using System;
using System.Threading;

namespace Numble
{
    class Program
    {
        #region Property
        static Player player;
        static Field field;
        public static int size = 7;
        public static int time = 1000;
        #endregion 
        static void Main()
        {
            Console.BufferHeight = Console.WindowHeight = 30;
            Console.BufferWidth = Console.WindowWidth = 33;
            NameGame.Name(size, time);
            ShowMenu();
        }
        public static void ShowMenu()
        {
            int choise = 0;
            Menu.PrintMenu(choise, size);

            ConsoleKeyInfo inputKey;
            
            while (true)
            {
                if (Console.KeyAvailable)
                {
                    inputKey = Console.ReadKey();
                    if (inputKey.Key == ConsoleKey.UpArrow)
                    {
                        choise--;
                        choise += 4;
                        choise %= 4;
                    }
                    else if (inputKey.Key == ConsoleKey.DownArrow)
                    {
                        choise++;
                        choise %= 4;
                    }
                    else if (inputKey.Key == ConsoleKey.Enter)
                    {
                        MakeChoose(choise, size);
                    }
                    Console.Clear();
                    Menu.PrintMenu(choise, size);
                }
            }
        }

        private static void MakeChoose(int choise, int n)
        {
            Console.Clear();
            switch (choise)
            {
                case 0:
                    StartGame(StartScreen.ChooseLevel(n));
                    break;
                case 1:
                    Menu.ReadHelp();
                    break;
                case 2:
                    Menu.ReadScores();
                    break;
                case 3:
                    //End screen
                    BYE.Bye(n, time);

                    Environment.Exit(1);
                    break;
            }
        }
        
        private static void StartGame(int level)
        {
            player = new Player(29, 32, level);
            field = new Field(29, 32, 0, level);

            int count = 0;

            player.Print();
            field.Print();

            while (true)
            {
                if (player.MovePlayer())
                {
                    if (player.isShoot)
                    {
                        Console.Clear();
                        player.isShoot = false;
                        player.Points += field.PutNewSymbol(player.Col, player.LastSymbol);
                        field.Print();
                    }
                    player.Print();
                }

                if (count == player.Wait)
                {
                    field.AddNewLine();
                    field.Print();
                    player.Print();
                    count = 0;
                }
                // матрицата да достига row-1 иначе има доблиране и човек се чуди какво става 
                if (field.isEndGame)
                {
                    //EndScreen  Калоян
                    Thread.Sleep(time);
                    Console.Clear();

                    EndGame.End(size, time);

                    EndGame.PointsAtTheEnd(player.Points, size, time);
                    return;
                }
                count++;
                Thread.Sleep(5);
            }
        }
    }
}
